package cn.pzhu.services.admin;

import cn.pzhu.model.Role;
import cn.pzhu.services.BaseServices;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dongp on 2016/4/25.
 */
public class RoleServices extends BaseServices {

    public List<Role> ajaxQuery() {
        List<Role> roles = Role.dao.find("select * from role");
        return roles;
    }

    public List<Role> ajaxQueryPage(String pageNumber, String pageSize, String search) {
        ArrayList arrts = new ArrayList();
        Map<String,Object> map = new HashMap<>();

        List<Role> roles = Role.dao.find("select r.* from role r where r.roleName like '%" + search + "%' limit "
                + pageNumber + "," + pageSize);
        arrts.add(roles);

        Role role = Role.dao.findFirst("select count(*) total from role r where r.roleName like '%"+search+"%'");
        Long total = role.getLong("total").longValue();
        map.put("total", total);
        arrts.add(map);
        return arrts;
    }

    public boolean ajaxUpdate(String roleID, String roleName, Integer roleValue) {

        boolean result = Role.dao.findById(roleID).set("roleName",roleName).set("roleValue",roleValue).update();

        return result;
    }

    public boolean ajaxAdd(String roleName, Integer roleValue) {

        // 检查是否重复
        Role role = (Role) Role.dao.findFirst("select count(*) count from role where roleName = ?",roleName);
        Long count = role.getLong("count").longValue();
        if(count > 0) {
            return false;
        }

        // 保存
        boolean result = new Role().set("roleName", roleName).set("roleValue",roleValue).save();
        return result;
    }

    public boolean ajaxDel(String roleID) {
        return Role.dao.deleteById(roleID);
    }

}
